import multiprocessing
import time
from multiprocessing import Process
from threading import Thread
from typing import Optional


class MyProcess(Process):
    MyType = Optional[Thread]

    def __init__(self):
        super(MyProcess, self).__init__()
        self.__proc: MyProcess.MyType = None
        self.__status = False
        self.__procQueue = multiprocessing.Queue(10)

    def __thread(self):
        nums = 0
        while nums <= 10 and not self.__status:
            self.__procQueue.put(nums)
            print(f"运行中[{nums}]")
            time.sleep(1)
            nums += 1
        self.__procQueue.put("END")

    def run(self) -> None:
        self.__proc = Thread(target=self.__thread)
        self.__proc.setDaemon(True)
        self.__proc.start()
        self.__proc.join()

    def getThread(self):
        return self.__proc

    def shutdown(self):
        self.terminate()

    def getObj(self):
        if self.__procQueue.empty():
            return None
        else:
            res = self.__procQueue.get()
            return res


if __name__ == '__main__':
    proc = MyProcess()
    proc.start()
    while True:
        res = proc.getObj()
        if res == None:
            continue
        elif res == "END":
            break
        else:
            print(res)
    proc.shutdown()
    proc.join()
